/**
 * File: NodeidfileTranslateController.java
 * Created by: mhaimel
 * Created on: May 4, 2010
 * CVS:  $Id: NodeidfileTranslateController.java 1.0 May 4, 2010 3:46:42 PM mhaimel Exp $
 */
package uk.ac.ebi.curtain.controller.graph;

import java.io.Serializable;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicReference;

import uk.ac.ebi.curtain.controller.ParallelQueuedController;
import uk.ac.ebi.curtain.controller.SimpleReport;
import uk.ac.ebi.curtain.management.IControllerReport;
import uk.ac.ebi.curtain.processor.graph.BucketContigid2Fileposition;
import uk.ac.ebi.curtain.utils.CurtainUncheckedException;

/**
 * @author mhaimel
 *
 */
public class ContigidfileTranslateController extends ParallelQueuedController<Serializable, Serializable, BucketContigid2Fileposition> {
	
	private final AtomicReference<ConcurrentMap<Long, Long>> map = new AtomicReference<ConcurrentMap<Long,Long>>(null);	
	
	protected ConcurrentMap<Long, Long> getMap() {
		ConcurrentMap<Long, Long> tmpMap = this.map.get();
		if(null == tmpMap){
			tmpMap = (ConcurrentMap<Long, Long>) getContext().getScratchBoard().get(Node2ContigFilePositionController.KEY);
			if(null == tmpMap){
				throw new CurtainUncheckedException("ID mapping between Node id and Contig Id is not registered!!!");
			}
			this.map.compareAndSet(null, tmpMap);
		}
		return tmpMap;
	}

	@Override
	protected BucketContigid2Fileposition buildController() {
		BucketContigid2Fileposition writer = getContext().getInjector().getInstance(BucketContigid2Fileposition.class);
		writer.setId2pos(getMap());
		return writer;
	}
	
	@Override
	protected void queueShutdown() {
		getContext().getScratchBoard().remove(Node2ContigFilePositionController.KEY);
		super.queueShutdown();
	}

	@Override
	protected IControllerReport getReturnCall() {
		return new SimpleReport("Finished with Processing node Ids!");
	}


	
}
